//
//  PlayerMoreViewController.swift
//  GhostWorld
//
//  Created by Kam on 2018/5/31.
//  Copyright © 2018 LKGood. All rights reserved.
//

import UIKit
import AlamofireImage

protocol PlayerMoreViewControllerDelegate {
  func dismissPlayerMoreViewController(action: PlayerMoreAction)
}

enum PlayerMoreAction {
  case none
  case displayMoreAudiosOfReader
  case displayMoreAudiosOfWriter
  case displayAudioList
  case addToPlaylist
  case share
}

class PlayerMoreViewController: UIViewController {
  
  // MARK: - IBOutlets
  
  @IBOutlet private weak var contentPanel: Panel!
  @IBOutlet private weak var contentView: UIView!
  @IBOutlet private weak var coverView: UIView!
  @IBOutlet private weak var coverImageView: UIImageView!
  @IBOutlet private weak var audioNameLabel: UILabel!
  @IBOutlet private weak var albumNameLabel: UILabel!
  @IBOutlet private weak var writerNameLabel: UILabel!
  @IBOutlet private weak var readerNameLabel: UILabel!
  @IBOutlet private weak var moreAudiosOfReaderButton: UIButton!
  @IBOutlet private weak var moreAudiosOfWriterButton: UIButton!
  @IBOutlet private weak var stackView: UIStackView!
  @IBOutlet private weak var addToPlaylistButton: UIButton!
  @IBOutlet private weak var shareButton: UIButton!
  
  @IBOutlet private weak var stackViewTopConstraint: NSLayoutConstraint!
  
  // MARK: - Private vars
  
  private var isMoreAudiosOfReaderButtonEnabled = true
  private var isMoreAudiosOfWriterButtonEnabled = true
  private var isAddToPlaylistButtonEnabled = true
  private var isShareButtonEnabled = true
  
  // MARK: - Private methods
  
  private func prepareUI() {
    view.backgroundColor = .clear
    let tapGr = UITapGestureRecognizer(target: self, action: #selector(dismissSelf))
    view.addGestureRecognizer(tapGr)
    
    contentPanel.layer.cornerRadius = 4
    let buttonDisableAlpha: CGFloat = 0.3
    
    if let album = NowPlayingManager.shared.album,
      let audio = NowPlayingManager.shared.audio {
      
      audioNameLabel.text = audio.name
      albumNameLabel.text = album.name
      if let url = album.cover.getURL(size: .thumb) {
        coverImageView.af_setImage(withURL: url)
      }
      
      if let count = album.writers?.count, count > 0 {
        let writerName = album.writers!.map{ ($0 as! Writer).name! }.joined(separator: ",")
        writerNameLabel.text = writerName
        moreAudiosOfWriterButton.setTitle("更多\(writerName)的节目(暂不可用)", for: .normal)
        moreAudiosOfWriterButton.alpha = 1
        isMoreAudiosOfWriterButtonEnabled = true
      } else {
        writerNameLabel.text = "未知"
        moreAudiosOfWriterButton.setTitle("更多作者的节目(暂不可用)", for: .normal)
        moreAudiosOfWriterButton.alpha = buttonDisableAlpha
        isMoreAudiosOfWriterButtonEnabled = false
      }
      
      if let count = album.readers?.count, count > 0 {
        let readerName = album.readers!.map{ ($0 as! Reader).name! }.joined(separator: ",")
        readerNameLabel.text = readerName
        moreAudiosOfReaderButton.setTitle("更多\(readerName)的节目(暂不可用)", for: .normal)
        moreAudiosOfReaderButton.alpha = 1
        isMoreAudiosOfReaderButtonEnabled = true
      } else {
        readerNameLabel.text = "未知"
        moreAudiosOfReaderButton.setTitle("更多播讲人的节目(暂不可用)", for: .normal)
        moreAudiosOfReaderButton.alpha = buttonDisableAlpha
        isMoreAudiosOfReaderButtonEnabled = false
      }
      
      addToPlaylistButton.alpha = 1
      isAddToPlaylistButtonEnabled = true
      shareButton.alpha = 1
      isShareButtonEnabled = true
    } else {
      addToPlaylistButton.alpha = buttonDisableAlpha
      isAddToPlaylistButtonEnabled = false
      shareButton.alpha = buttonDisableAlpha
      isShareButtonEnabled = false
    }
  }
  
  private func prepareForAnimation() {
    contentView.alpha = 0
    stackView.alpha = 0
    contentPanel.alpha = 0
    contentPanel.frame = initialFrame
    
    stackViewTopConstraint.constant = -44
  }
  
  private func runAnimation() {
    UIView.animate(withDuration: 0.3, animations: {
      self.contentPanel.alpha = 1
      self.contentPanel.frame = CGRect (
        x: 20,
        y: self.contentView.y,
        width: self.view.width - 40,
        height: 274
      )
    }) { _ in
      self.stackViewTopConstraint.constant = 16
      UIView.animate(withDuration: 0.2, animations: {
        self.stackView.alpha = 1
        self.contentView.alpha = 1
        self.view.layoutIfNeeded()
      })
    }
  }
  
  @objc private func dismissSelf() {
    delegate?.dismissPlayerMoreViewController(action: .none)
  }
  
  // MARK: - Public vars
  
  var initialFrame: CGRect = .zero
  var delegate: PlayerMoreViewControllerDelegate?
  
  // MARK: - Lifecycle
  
  override func viewDidLoad() {
    super.viewDidLoad()
    
    prepareUI()
  }
  
  override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    
    prepareForAnimation()
  }
  
  override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    
    runAnimation()
  }
  
  override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
  }
  
  // MARK: - IBActions
  
  @IBAction func showMoreAudiosOfReader() {
    if isMoreAudiosOfReaderButtonEnabled {
      delegate?.dismissPlayerMoreViewController (
        action: .displayMoreAudiosOfReader
      )
    }
  }
  
  @IBAction func showMoreAudiosOfWriter() {
    if isMoreAudiosOfWriterButtonEnabled {
      delegate?.dismissPlayerMoreViewController (
        action: .displayMoreAudiosOfWriter
      )
    }
  }
  
  @IBAction func showAudioList() {
    delegate?.dismissPlayerMoreViewController (
      action: .displayAudioList
    )
  }
  
  @IBAction func addToPlaylist() {
    if isAddToPlaylistButtonEnabled {
      delegate?.dismissPlayerMoreViewController (
        action: .addToPlaylist
      )
    }
  }
  
  @IBAction func share() {
    if isShareButtonEnabled {
      delegate?.dismissPlayerMoreViewController (
        action: .share
      )
    }
  }
  
  // MARK: - Public methods
  
  
}
